/**
 * Contains {@link ActiveElement}
 */
package logic.pathelements;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Arrays;
import logic.connectors.Connector;
import logic.connectors.NetworkInterface;

/**
 * Active network element contains network interfaces {@link NetworkInterface}
 * @author Alexey Akimov <frinatella@gmail.com>
 */
public abstract class ActiveElement extends PathElement {
    
    /**
     * this element name
     */
    protected String name;
    
    /**
     * Constructor instances this element id and network interfaces according to its parameters
     * @param id this element id
     * @param name this element name
     * @param networkInterfacesCount this element network interfaces count
     */
    public ActiveElement(Integer id, String name, int networkInterfacesCount) throws Exception {
        super(id, networkInterfacesCount);
        this.name = name;
        for (int i = 0; i < networkInterfacesCount; ++i) {
            connectors.add(i, new NetworkInterface(this));
        }
    }
    
    /**
     * Get one of network interfaces ip
     * @param networkInterface this element network interface number
     * @return this element ip address
     */
    public Inet4Address getIp(int networkInterfaceNumber) {
        return ((NetworkInterface) connectors.get(networkInterfaceNumber)).getIp();
    }
    
    /**
     * Searches network interface by its ip
     * @param ip searched network interface ip
     * @return finded network interface or null
     */
    public NetworkInterface getNetworkInterface(Inet4Address ip) {
        for (Connector networkInterface: connectors) {
            if (Arrays.equals(((NetworkInterface) networkInterface).getIp().getAddress(), ip.getAddress())) {
                return (NetworkInterface) networkInterface;
            }
        }
        return null;
    }
    
    /**
     * Searches network interface by its ip text representation
     * @param ip searched network interface ip text representation
     * @return finded network interface or null
     */
    public NetworkInterface getNetworkInterface(String ip) throws UnknownHostException {
        return getNetworkInterface((Inet4Address) Inet4Address.getByName(ip));
    }
    
    /**
     * Get one of network interfaces ip text representation
     * @param networkInterface this element network interface number
     * @return this element ip address text representation
     */
    public String getIpTextRepresentation(int networkInterfaceNumber) {
        return ((NetworkInterface) connectors.get(networkInterfaceNumber)).getIpTextRepresentation();
    }
    /**
     * Set one of network interfaces ip
     * @param networkInterfaceNumber this element network interface number
     * @param ip this element ip address
     */
    public void setIp(int networkInterfaceNumber, Inet4Address ip) {
        ((NetworkInterface) connectors.get(networkInterfaceNumber)).setIp(ip);
    }
    
    /**
     * Set one of network interfaces ip by its text representation
     * @param networkInterfaceNumber this element network interface number
     * @param ip this element ip address text representation
     */
    public void setIp(int networkInterfaceNumber, String ip) throws UnknownHostException {
        ((NetworkInterface) connectors.get(networkInterfaceNumber)).setIp(ip);
    }
}
